package com.csust.automaticteach.utils.template.generate;

import com.csust.automaticteach.app.teacher.ExamGenerateApp;
import com.csust.automaticteach.model.dto.generateexam.Exam;
import com.csust.automaticteach.model.enums.GenerateTypeEnum;
import com.csust.automaticteach.exception.ErrorCode;
import com.csust.automaticteach.exception.ThrowUtils;
import com.csust.automaticteach.model.dto.generateexam.AddGenerateExamForAiRequest;
import com.csust.automaticteach.model.dto.generateexam.AddGenerateExamRequest;
import com.csust.automaticteach.model.entity.Exercises;
import com.csust.automaticteach.model.entity.ExercisesSets;
import com.csust.automaticteach.model.entity.SetExercises;
import com.csust.automaticteach.model.vo.generateexam.GenerateExamResponse;
import com.csust.automaticteach.service.ExercisesService;
import com.csust.automaticteach.service.ExercisesSetsService;
import com.csust.automaticteach.service.GenerateExamService;
import com.csust.automaticteach.service.SetExercisesService;
import com.csust.automaticteach.utils.ParseGenerateExerciseJsonUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.io.Serial;
import java.io.Serializable;
import java.util.List;
import static cn.hutool.json.JSONUtil.toJsonStr;

@Service
public class ExamGenerator extends AbstractGeneratorTemplate<AddGenerateExamRequest, GenerateExamResponse> {
    private final ExamGenerateApp examGenerateApp;
    private final ParseGenerateExerciseJsonUtil parseGenerateExerciseJsonUtil;
    private AddGenerateExamForAiRequest aiRequest;

    // 构造函数注入所有依赖
    public ExamGenerator(ExercisesService exercisesService,
                         ExercisesSetsService exercisesSetsService,
                         SetExercisesService setExercisesService,
                         ExamGenerateApp examGenerateApp,
                         ParseGenerateExerciseJsonUtil parseGenerateExerciseJsonUtil) {
        super(exercisesService, exercisesSetsService, setExercisesService);
        this.examGenerateApp = examGenerateApp;
        this.parseGenerateExerciseJsonUtil = parseGenerateExerciseJsonUtil;
    }

    @Override
    protected void prepareGenerateParams() {
        this.title = loginUser.getUserName() + "老师的教学测试";
//        this.aiRequest = generateExamService.getReferenceByRequest(request);
//        this.aiRequest.setExams(request.getExams());
//        this.aiRequest.setDifficultyLevel(request.getDifficultyLevel());
        // 复用基类的exams属性
        BeanUtils.copyProperties(request, aiRequest);
    }

    @Override
    protected String callGenerateService() {
        return examGenerateApp.doGenerate(aiRequest, chatId);
    }

    @Override
    protected List<GenerateExamResponse> parseResult(String result) {
        return parseGenerateExerciseJsonUtil.parseJson(result, GenerateExamResponse.class);
    }

    @Override
    protected List<Exercises> convertToExercises(List<GenerateExamResponse> responseList) {
        String difficulty = request.getDifficultyLevel();

        return responseList.stream()
                .map(examResponse -> {
                    Exercises exercises = new Exercises();
                    BeanUtils.copyProperties(examResponse, exercises);
                    exercises.setOptions(toJsonStr(examResponse.getOptions()));
                    exercises.setKnowledgePoint(examResponse.getKnowledgePoints().get(0));
                    exercises.setUserId(userId);
                    exercises.setDifficulty(difficulty);
                    exercises.setTitle(title + "题");

                    return exercises;
                })
                .toList();
    }

    @Override
    protected Long saveData(List<Exercises> exercisesList) {
        // 保存练习题
        boolean saved1 = super.getExercisesService().saveBatch(exercisesList);
        // 创建套题
        ExercisesSets exercisesSets = ExercisesSets.builder()
                .title(title)
                .userId(userId)
                .setType(GenerateTypeEnum.EXAMINATION.getValue())
                .build();
        boolean saved2 = super.getExercisesSetsService().save(exercisesSets);
        // 建立关联
        List<SetExercises> setExercisesList = super.getSetExercisesService()
                .getSetExercisesList(exercisesList, exercisesSets.getId());
        boolean saved3 = super.getSetExercisesService().saveBatch(setExercisesList);
        // 校验
        ThrowUtils.throwIf(!saved1 || !saved2 || !saved3, ErrorCode.OPERATION_ERROR);
        //返回套题id
        return exercisesSets.getId();
    }
}